﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;

namespace VisionCloud.Utility
{
    public class Logger
    {
        /// <summary>
        /// 获取日志对象
        /// </summary>
        /// <returns></returns>
        public static ILogger GetLogger()
        {
            ILogger logger = StartupServer.ServiceProvider.GetRequiredService<ILogger<Logger>>();
            return logger;
        }

        /// <summary>
        /// 获取指定类型的日志对象
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <returns></returns>
        public static ILogger<T> GetLogger<T>()
        {
            return StartupServer.ServiceProvider.GetRequiredService<ILogger<T>>();
        }

        /// <summary>
        /// 获取指定类型的日志对象
        /// </summary>
        /// <param name="type">类型的属性</param>
        /// <returns></returns>
        public static ILogger GetLogger(Type type)
        {
            if (type == null)
                return GetLogger();
            ILoggerFactory factory = StartupServer.ServiceProvider.GetRequiredService<ILoggerFactory>();
            var logger = factory.CreateLogger(type);
            return logger;
        }
        public static void Trace(string message)
        {
            var logger = GetLogger();
            logger.LogTrace(message);
        }
        /// <summary>
        /// 普通日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="exception">错误</param>
        public static void Info(string message, Exception exception = null)
        {
            var logger = GetLogger();
            if (exception == null)
                logger.LogInformation(message);
            else
                logger.LogInformation(exception, message);
        }

        /// <summary>
        /// 普通日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="type">指定类型</param>
        /// <param name="exception">错误</param>
        public static void Info(string message, Type type, Exception exception = null)
        {
            var logger = GetLogger(type);
            if (exception == null)
                logger.LogInformation(message);
            else
                logger.LogInformation(exception, message);
        }

        /// <summary>
        /// 普通日志
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="message">日志内容</param>
        /// <param name="exception">错误</param>
        public static void Info<T>(string message, Exception exception = null)
        {
            var logger = GetLogger<T>();
            if (exception == null)
                logger.LogInformation(message);
            else
                logger.LogInformation(exception, message);
        }

        /// <summary>
        /// 告警日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="exception">错误</param>
        public static void Warn(string message, Exception exception = null)
        {
            var logger = GetLogger();
            if (exception == null)
                logger.LogWarning(message);
            else
                logger.LogWarning(exception, message);
        }

        /// <summary>
        /// 告警日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="type">指定类型</param>
        /// <param name="exception">错误</param>
        public static void Warn(string message, Type type, Exception exception = null)
        {
            var logger = GetLogger(type);
            if (exception == null)
                logger.LogWarning(message);
            else
                logger.LogWarning(exception, message);
        }

        /// <summary>
        /// 告警日志
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="message">日志内容</param>
        /// <param name="exception">错误</param>
        public static void Warn<T>(string message, Exception exception = null)
        {
            var logger = GetLogger<T>();
            if (exception == null)
                logger.LogWarning(message);
            else
                logger.LogWarning(exception, message);
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="exception">错误</param>
        public static void Error(string message, Exception exception = null)
        {
            var logger = GetLogger();
            if (exception == null)
                logger.LogError(message);
            else
                logger.LogError(exception, message);
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="type">指定类型</param>
        /// <param name="exception">错误</param>
        public static void Error(string message, Type type, Exception exception = null)
        {
            var logger = GetLogger(type);
            if (exception == null)
                logger.LogError(message);
            else
                logger.LogError(exception, message);
        }

        /// <summary>
        /// 错误日志
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="message">日志内容</param>
        /// <param name="exception">错误</param>
        public static void Error<T>(string message, Exception exception = null)
        {
            var logger = GetLogger<T>();
            if (exception == null)
                logger.LogError(message);
            else
                logger.LogError(exception, message);
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="exception">错误</param>
        public static void Debug(string message, Exception exception = null)
        {
            var logger = GetLogger();
            if (exception == null)
                logger.LogDebug(message);
            else
                logger.LogDebug(exception, message);
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="type">指定类型</param>
        /// <param name="exception">错误</param>
        public static void Debug(string message, Type type, Exception exception = null)
        {
            var logger = GetLogger(type);
            if (exception == null)
                logger.LogDebug(message);
            else
                logger.LogDebug(exception, message);
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="message">日志内容</param>
        /// <param name="exception">错误</param>
        public static void Debug<T>(string message, Exception exception = null)
        {
            var logger = GetLogger<T>();
            if (exception == null)
                logger.LogDebug(message);
            else
                logger.LogDebug(exception, message);
        }
    }
}